<?php

namespace backend\controllers;

use common\helps\AsrSearch;
use common\helps\Tools;
use Yii;
use app\models\AsrCall;
use app\models\search\AsrCallSearch;
use yii\data\Pagination;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * AsrCallController implements the CRUD actions for AsrCall model.
 */
class AsrCallController extends CommonController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all AsrCall models.
     * @return mixed
     */
    public function actionIndex()
    {
//        $a = new AsrSearch();
//        $a->search_keywork();
//        exit;
        $searchModel = new AsrCallSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
        $query = $dataProvider->query;
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        if (isset($_POST['pageSize'])) {//设置了参数，就把参数给pageSize session
            setcookie('pageSize', $_POST['pageSize']);
        }
        if (!empty($_COOKIE['pageSize'])) {
            $pages->pageSize = (int)$_COOKIE['pageSize'];
        } else {
            $pages->pageSize = 5;
        }
//        $models = $query->offset($pages->offset)
//            ->limit($pages->limit)
//            ->all();
        $dataProvider->pagination = $pages;
        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
            'page' => $pages,
        ]);
    }

    /**
     * Displays a single AsrCall model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new AsrCall model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new AsrCall();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->call_id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing AsrCall model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            echo Yii::$app->session->setFlash('success','修改成功！');
            return $this->redirect(['view', 'id' => $model->call_id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing AsrCall model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
        echo Yii::$app->session->setFlash('danger','删除成功！');
        return $this->redirect(['index']);
    }

    /**
     * Finds the AsrCall model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return AsrCall the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = AsrCall::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }


    /**
     * 禁用该电话线路
     * @param $id
     * @return AsrCall|null
     * @throws NotFoundHttpExceptions
     */
    public function actionForbidden($id)
    {
        $asrCall = new AsrCall();
        $result = $asrCall->findOne($id);

        if($result->status == 1){
            $result->status = 0;
            $result->save();
            return $this->redirect(['asr-call/index']);
        }else{
            $result->status = 1;
            $result->save();
            return $this->redirect(['asr-call/index']);
        }

    }
}
